// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Menu DPad Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

// value0 is skipped over
private alias object.value1 : object.pressedLR


// ========================
// Events
// ========================

event ObjectUpdate
	if MenuControl_optionsMenuOpen == false
		temp0 = screen.xcenter
		temp0 -= 56
		CheckTouchRect(0, 96, temp0, screen.ysize)
		if checkResult > -1
			arrayPos0 = checkResult
			temp0 = touchscreen[arrayPos0].xpos
			temp0 -= 64
			temp1 = touchscreen[arrayPos0].ypos
			temp1 -= 202
			ATan2(temp2, temp0, temp1)
			temp2 += 0x20
			temp2 &= 0xFF
			temp2 >>= 6
			switch temp2
			case 0
				keyDown[0].right = true
				if object.pressedLR == false
					keyPress[0].right |= keyDown[0].right
				end if
				object.pressedLR = keyDown[0].right
				break

			case 1
				keyDown[0].down = true
				break

			case 2
				keyDown[0].left = true
				if object.pressedLR == false
					keyPress[0].left |= keyDown[0].left
				end if
				object.pressedLR = keyDown[0].left
				break

			case 3
				keyDown[0].up = true
				break
			end switch
		else
			object.pressedLR = false
		end if
	end if
end event


event ObjectDraw
	if MenuControl_optionsMenuOpen == false
		if keyDown[0].up == true
			DrawSpriteScreenXY(1, 64, 202)
		else
			DrawSpriteScreenXY(0, 64, 202)
		end if
	
		if keyDown[0].down == true
			DrawSpriteScreenXY(3, 64, 202)
		else
			DrawSpriteScreenXY(2, 64, 202)
		end if
	
		if keyDown[0].left == true
			DrawSpriteScreenXY(5, 64, 202)
		else
			DrawSpriteScreenXY(4, 64, 202)
		end if
	
		if keyDown[0].right == true
			DrawSpriteScreenXY(7, 64, 202)
		else
			DrawSpriteScreenXY(6, 64, 202)
		end if
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	
	// In initial verions this section used #platorm checks, but now regular checks are done instead
	foreach (TypeName[Menu DPad], arrayPos0, ALL_ENTITIES)
		switch engine.deviceType
		case STANDARD
			// If on a standard platform, then touch controls aren't needed
			object[arrayPos0].type = TypeName[Blank Object]
			break

		case MOBILE
			object[arrayPos0].priority = PRIORITY_ACTIVE
			break
			
		end switch
	next

	SpriteFrame(-10, -30, 20, 20, 138, 1)	// 0 - Blue up arrow
	SpriteFrame(-10, -30, 20, 20, 138, 43)	// 1 - Red up arrow
	SpriteFrame(-10, 10, 20, 20, 159, 1)	// 2 - Blue down arrow
	SpriteFrame(-10, 10, 20, 20, 159, 43)	// 3 - Red down arrow
	SpriteFrame(-30, -10, 20, 20, 138, 22)	// 4 - Blue left arrow
	SpriteFrame(-30, -10, 20, 20, 138, 64)	// 5 - Red left arrow
	SpriteFrame(10, -10, 20, 20, 159, 22)	// 6 - Blue right arrow
	SpriteFrame(10, -10, 20, 20, 159, 64)	// 7 - Red right arrow
end event


event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
	DrawSprite(3)
end event


event RSDKLoad
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-10, -30, 20, 20, 138, 1)	// 0 - Blue up arrow
	SpriteFrame(-10, 10, 20, 20, 159, 1)	// 1 - Blue down arrow
	SpriteFrame(-30, -10, 20, 20, 138, 22)	// 2 - Blue left arrow
	SpriteFrame(10, -10, 20, 20, 159, 22)	// 3 - Blue right arrow
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event